#include <stdio.h>

// 使用异或的方式交换数组首尾的元素
void reverse_array(int *array, int n) {
    // first，last 作为数组的首尾指针
    int first, last;
    for (first = 0, last = n - 1; first < last; first++, last--) {
        array[first] ^= array[last];
        array[last] ^= array[first];
        array[first] ^= array[last];
    }
}

int main() {
    int array[] = {1, 2, 3, 4, 5};
    int n = sizeof(array) / sizeof(array[0]);
    printf("Before reverse:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");

    reverse_array(array, n);

    printf("After reverse:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");

    return 0;
}

// Usage: gcc -o inplace_swap.out inplace_swap.c
// Run with: ./inplace_swap.out
// The output will show the original array and the reversed array.
// Output:
// Before reverse:
// 1 2 3 4 5
// After reverse:
// 5 4 3 2 1
//
// The output will show the original array and the reversed array.
// The reverse_array function uses the XOR swap algorithm to swap elements
// in place without using a temporary variable.
// This is a simple demonstration of how to reverse an array in C.
// The XOR swap algorithm is not commonly used in practice due to its
// complexity and potential for confusion, but it is an interesting
// example of how to manipulate bits and perform in-place swaps.
// The code is a simple demonstration of how to reverse an array in C.
// The reverse_array function uses the XOR swap algorithm to swap elements
// in place without using a temporary variable.